import 'package:flui/flui.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:fzcgx_app/common/adapter_utils.dart';
import 'package:fzcgx_app/network/http.dart';
import 'package:fzcgx_app/ui/base/base_view_model.dart';
import 'package:fzcgx_app/ui/my/factory/model/factory_production_capacity_model.dart';

class FactoryProductionCapacityViewModel extends BaseViewModel {
  FactoryProductionCapacityModel model;
  TextEditingController caijianController = TextEditingController();
  TextEditingController zhengjianController = TextEditingController();
  TextEditingController kunbaoliuController = TextEditingController();
  TextEditingController danjianliunController = TextEditingController();
  TextEditingController zongrenshuController = TextEditingController();
  TextEditingController richanliangController = TextEditingController();
  FactoryProductionCapacityViewModel() {
    this.setState(ViewState.Loading);
  }
  void requestApi() {
    Http.instance.post<FactoryProductionCapacityModel>('output/daily', null,
        onSuccess: (data) {
      if (null != data) {
        this.model = data;
        this.caijianController.text = model.caijian;
        this.zhengjianController.text = model.zhengjian;
        this.kunbaoliuController.text = model.kunbaoliu;
        this.danjianliunController.text = model.danjianliu;
        this.zongrenshuController.text = model.zongrenshu;
        if (model.selectModel != null) {
          richanliangController.text = model.selectModel.name;
        }
        this.setState(ViewState.Success);
      } else {
        this.model = FactoryProductionCapacityModel();
        this.setState(ViewState.Success);
      }
    }, onError: (error) {
      this.setState(ViewState.Error);
    }, onFail: (fail) {
      this.setState(ViewState.Error);
    });
  }

  void showPicker(
    BuildContext context,
  ) {
    List lists = List();
    model.options.forEach((element) {
      lists.add(element.name);
    });

    Picker(
        adapter: PickerDataAdapter<String>(
          pickerdata: lists,
        ),
        changeToFirst: true,
        hideHeader: false,
        selectedTextStyle: TextStyle(
          fontSize: AdapterUtils.getSp(18),
          color: Color(0xff000000),
          fontWeight: FontWeight.w500,
        ),
        textStyle: TextStyle(
          fontSize: AdapterUtils.getSp(16),
          color: Color(0xff333333),
          fontWeight: FontWeight.w500,
        ),
        title: Text(
          '选择日产量',
          style: TextStyle(
            fontSize: AdapterUtils.getSp(14),
            color: Color(0xff999999),
            fontWeight: FontWeight.w500,
          ),
        ),
        onConfirm: (Picker picker, List value) {
          int index = value[0];
          this.model.selectModel = model.options[index];
          richanliangController.text=this.model.selectModel.name;
          notifyListeners();
        }).showModal(context);
  }

  void postApi(BuildContext context, Function callback) {
    if (this.caijianController.text.isEmpty) {
      FLToast.showText(text: '请输入裁剪人数');
      return;
    }
    if (this.zhengjianController.text.isEmpty) {
      FLToast.showText(text: '请输入整件人数');
      return;
    }
    if (this.kunbaoliuController.text.isEmpty) {
      FLToast.showText(text: '请输入捆包流人数');
      return;
    }
    if (this.danjianliunController.text.isEmpty) {
      FLToast.showText(text: '请输入单件流人数');
      return;
    }
    if (this.zongrenshuController.text.isEmpty) {
      FLToast.showText(text: '请输入全厂总人数');
      return;
    }
    if (this.model.selectModel.id.isEmpty) {
      FLToast.showText(text: '请选择日产量');
      return;
    }
    var dismiss = FLToast.loading(text: '正在保存...');
    Http.instance.post(
        'output/save',
        {
          'caijian': caijianController.text,
          'zhengjian': zhengjianController.text,
          'kunbaoliu': kunbaoliuController.text,
          'danjianliu': danjianliunController.text,
          'zongrenshu': zongrenshuController.text,
          'richanliang': this.model.selectModel.id
        },
        onSuccess: (data) {
          FLToast.showSuccess(text: '保存成功');
          callback();
          Navigator.pop(context);
        },
        onError: (error) {},
        onFail: (fail) {},
        complete: () {
          dismiss();
        });
  }
}
